package org.example.supplychain.service;

import org.example.supplychain.domain.EntryExitRecords;
import org.example.supplychain.domain.TotalMaterial;
import org.example.supplychain.mapper.EntryExitRecordsMapper;
import org.example.supplychain.mapper.OrderMapper;
import org.example.supplychain.mapper.TotalMaterialMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

@Service
public class TotalMaterialService {
    @Autowired
    private TotalMaterialMapper totalMaterialMapper;
    @Autowired
    private EntryExitRecordsMapper entryExitRecordsMapper;
    @Autowired
    private OrderMapper orderMapper;

    /**
     * 调用时刷新告警状态
     */
    public void updateWaringState() {
        List<TotalMaterial> allTotalMaterial = totalMaterialMapper.getAllTotalMaterial();
        for (TotalMaterial tm : allTotalMaterial) {
            tm.setWarningState(tm.getMaterialTotalNumber() < tm.getMinimumStock());
            totalMaterialMapper.updateWarningStateByMaterialName(tm);
        }
    }

    /**
     * 调用时修改原料最低库存警戒值
     */
    public void updateMinimumStock(String materialName, Long materialMinimum) {
        TotalMaterial tm = totalMaterialMapper.getTotalMaterialByMaterialName(materialName);
        tm.setMinimumStock(materialMinimum);
        updateWaringState();
    }

    /**
     * 调用时刷新原料总量（根据出入库记录）
     */
    public void updateMaterialTotalNumber() {
        List<String> materialNameList = orderMapper.getAllMaterialName();
        HashMap<String, Long> hm = new HashMap<>();
        for (String materialName : materialNameList) {
            hm.put(materialName, 0L);
        }
        List<EntryExitRecords> entryExitRecordsList = entryExitRecordsMapper.getAllEntryExitRecords();
        for (EntryExitRecords entryExitRecords : entryExitRecordsList) {
            if (entryExitRecords.getRecordStatus().equals("入库"))
                hm.replace(entryExitRecords.getMaterialName(), hm.get(entryExitRecords.getMaterialName()) + entryExitRecords.getMaterialNumber());
            else {
                hm.replace(entryExitRecords.getMaterialName(), hm.get(entryExitRecords.getMaterialName()) - entryExitRecords.getMaterialNumber());
            }
        }
        for (String s : hm.keySet()) {
            TotalMaterial totalMaterial = totalMaterialMapper.getTotalMaterialByMaterialName(s);
            totalMaterial.setMaterialTotalNumber(hm.get(s));
            totalMaterialMapper.updateMaterialTotalNumberByMaterialName(totalMaterial);
        }
        updateWaringState();
    }

    /**
     * 添加原料总量
     *
     * @param totalMaterial 原料总量
     */
    public void addTotalMaterial(TotalMaterial totalMaterial) {
        totalMaterialMapper.addTotalMaterial(totalMaterial);
    }

    /**
     * 通过原料名获取原料总量信息
     *
     * @param totalMaterialName 原料名
     * @return 总量
     */
    public TotalMaterial getTotalMaterialByName(String totalMaterialName) {
        return totalMaterialMapper.getTotalMaterialByMaterialName(totalMaterialName);
    }

    /**
     * 根据原料名返回警戒值
     *
     * @param materialName 原料名
     * @return 警戒值
     */
    public long getMinimumStock(String materialName) {
        TotalMaterial tm = totalMaterialMapper.getTotalMaterialByMaterialName(materialName);
        return tm.getMinimumStock();
    }

    public List<TotalMaterial> getAllTotalMaterial() {
        return totalMaterialMapper.getAllTotalMaterial();
    }

    public void updateMinimumStockByMaterialName(TotalMaterial totalMaterial) {
        totalMaterialMapper.updateMinimumStockByMaterialName(totalMaterial);
    }

    public void updateMaterialTotalNumberByMaterialName(TotalMaterial totalMaterial) {
        totalMaterialMapper.updateMaterialTotalNumberByMaterialName(totalMaterial);
    }
}
